/*
 * net_protocol.h
 *
 *  Created on: 2023年8月5日
 *      Author: fjx
 */

#ifndef NET_NET_PROTOCOL_H_
#define NET_NET_PROTOCOL_H_
#include "mac/RBFCommissioning.h"
#include "mac_transmitter.h"
#include "mac_receiver.h"

typedef enum{
  EN_COMMISSIONG_IDLE,
  EN_COMMISSIONG_JOIN_NODE_PAGING_BUILD_PACKET,
  EN_COMMISSIONG_JOIN_NODE_PAGING_NET_AE,
  EN_COMMISSIONG_JOIN_NODE_PAGING_NET_DATA,  //往net层发送；测试时，进行数据copy
  EN_COMMISSIONG_JOIN_NODE_PAGING_NET_DE,
  EN_COMMISSIONG_JOIN_NODE_PAGING_PACKET_PARSE,


  EN_COMMISSIONG_GW_ALLOW_JOIN_NET_BUILD_PACKET,
  EN_COMMISSIONG_GW_ALLOW_JOIN_NET_NET_AE,
  EN_COMMISSIONG_GW_ALLOW_JOIN_NET_NET_DATA,
  EN_COMMISSIONG_GW_ALLOW_JOIN_NET_NET_DE,
  EN_COMMISSIONG_GW_ALLOW_JOIN_NET_PACKET_PARSE,

  EN_COMMISSIONG_JOIN_CONFIRM_BUILD_PACKECT,
  EN_COMMISSIONG_JOIN_CONFIRM_NET_AE,
  EN_COMMISSIONG_JOIN_CONFIRM_NET_DATA,
  EN_COMMISSIONG_JOIN_CONFIRM_NET_DE,
  EN_COMMISSIONG_JOIN_CONFIRM_PACKECT_PARSE,
  EN_COMMISSIONG_JOIN_CONFIRM_SUCCESS,



}COMMISSIONG_STATUS_EN;

typedef enum{


  EN_MSG_STATUS_IDLE,
  EN_MSG_STATUS_JOIN_NODE_PAGING_BUILD_PACKET,
  EN_MSG_STATUS_JOIN_NODE_PAGING_NET_AE,
  EN_MSG_STATUS_JOIN_NODE_PAGING_NET_DATA,  //往net层发送；测试时，进行数据copy
  EN_MSG_STATUS_JOIN_NODE_PAGING_NET_DE,
  EN_MSG_STATUS_JOIN_NODE_PAGING_PACKET_PARSE,

  EN_MSG_STATUS_GW_ALLOW_JOIN_NET_BUILD_PACKET,
  EN_MSG_STATUS_GW_ALLOW_JOIN_NET_NET_AE,
  EN_MSG_STATUS_GW_ALLOW_JOIN_NET_NET_DATA,
  EN_MSG_STATUS_GW_ALLOW_JOIN_NET_NET_DE,
  EN_MSG_STATUS_GW_ALLOW_JOIN_NET_PACKET_PARSE,

  EN_MSG_STATUS_JOIN_CONFIRM_BUILD_PACKECT,
  EN_MSG_STATUS_JOIN_CONFIRM_NET_AE,
  EN_MSG_STATUS_JOIN_CONFIRM_NET_DATA,
  EN_MSG_STATUS_JOIN_CONFIRM_NET_DE,
  EN_MSG_STATUS_JOIN_CONFIRM_PACKECT_PARSE,
  EN_MSG_STATUS_JOIN_CONFIRM_SUCCESS,

}NET_MESSAGE_STATUS_EN;

typedef enum{
  NET_DATA_STATUS_SUCCESS = 0,
  NET_DATA_STATUS_FAIL,

}NET_DATA_STATUS_EN;

typedef struct{
  uint8_t mac[8];
  uint8_t deviceType;
  uint8_t rfmid;
  uint8_t codeA[RBF_KEY_LENGTH_MAX];
  uint8_t codeAB[RBF_KEY_LENGTH_MAX];
}node_join_info_t;

#define DATA_LINK_LENGTH              255
typedef struct{
  uint8_t down[DATA_LINK_LENGTH];     //明文数据
  uint8_t up[DATA_LINK_LENGTH];       //明文数据
  uint8_t buff[DATA_LINK_LENGTH];     //加解密数据缓存，实际发送的buff
}packet_t;

typedef struct{
//  COMMISSIONG_STATUS_EN status;
  node_join_info_t join_info;
  //RFM
#if defined (RFM_DEVICE)
  NET_MESSAGE_STATUS_EN rfmStatus;
  uint8_t rfmid;
  uint8_t codeB[RBF_KEY_LENGTH_MAX];
  packet_t rfm;
#endif

  //HUB
#if defined (HUB_DEVICE)
  NET_MESSAGE_STATUS_EN hubStatus;
  packet_t hub;
#endif
}join_t;


void Commissiong_join_node_paging_packet(uint8_t cn);
void Commissiong_join_confirm_packet(void);
void Commissiong_gw_allow_join_net_packt(void);
void txStatecallbackCommissiong (uint8_t** ppPacket,MAC_TX_STATE_E currState,void* pData);
void txStatecallbackAck (uint8_t** ppPacket,MAC_TX_STATE_E currState,void* pData);

void txStatecallbackAdhocDown0 (uint8_t** ppPacket,MAC_TX_STATE_E currState,void* pData);

void RxCallback(RF_PACKET_ST* pPacket,MAC_RX_STATE_E currState,void* pData);

/*
 *brief 获取注册状态
 *return = EN_MSG_STATUS_JOIN_CONFIRM_SUCCESS,注册成功
 *note:   如果return为EN_MSG_STATUS_JOIN_CONFIRM_SUCCESS,再调用clear_register_status
 */
NET_MESSAGE_STATUS_EN get_register_status(void);
void set_register_status(NET_MESSAGE_STATUS_EN status);
/*
 *@brief 清空注册状态
 */
void clear_register_status(void);

void netKeyInit(void);

void set_cmd_ack(uint8_t md_ack);
/*
 * net数据解包结果
 * return = NET_DATA_STATUS_SUCCESS,解包成功;
 *        = NET_DATA_STATUS_FAIL,解包失败
 */
NET_DATA_STATUS_EN get_net_data_status (void);

void set_net_data_status (NET_DATA_STATUS_EN status);

#endif /* NET_NET_PROTOCOL_H_ */
